package com.infoland.afanmall.model.order.dialog;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutCompat;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.guiGe.entity.ViewHolder;
import com.infoland.afanmall.model.order.activity.OrderDownActivity;
import com.infoland.afanmall.model.order.activity.PayErroeActivity;
import com.infoland.afanmall.model.order.activity.PaySuccessActivity;
import com.infoland.afanmall.base.dialog.BaseNiceDialogFragment;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.UserKit;
import com.infoland.afanmall.model.order.entity.OrderByResponse;

import butterknife.BindView;

/**
 * 直接购买
 * 支付弹出层
 */
@SuppressLint("ValidFragment")
public class PayDialogBy extends BaseNiceDialogFragment {
    @BindView(R.id.pay_close)
    ImageView payClose;
    @BindView(R.id.pay_price)
    TextView payPrice;
    @BindView(R.id.pay_img_wei_xin)
    ImageView payImgWeiXin;
    @BindView(R.id.pay_wei_xin)
    LinearLayoutCompat payWeiXin;
    @BindView(R.id.pay_img_jian_hang)
    ImageView payImgJianHang;
    @BindView(R.id.pay_jian_hang)
    LinearLayoutCompat payJianHang;
    @BindView(R.id.pay_zhi_fu_bt)
    Button payZhiFuBt;
    OrderByResponse.ObjectBean object;
    OrderDownActivity downCartActivity;
    String type;

    public PayDialogBy(OrderByResponse.ObjectBean object, Object downCart, String type) {
        this.object = object;
        this.downCartActivity = (OrderDownActivity) downCart;
        this.type = type;
    }

    @Override
    public int intLayoutId() {
        return R.layout.open_pay;
    }

    @Override
    public void convertView(ViewHolder holder, BaseNiceDialogFragment dialog) {
        payPrice.setText("￥" + object.getPaymentAmount());
        payZhiFuBt.setOnClickListener(v -> {
            payZhiFuBt.setEnabled(false);
            startPay();
            ToolsKit.setSleep(payZhiFuBt, 3000);
        });
        payClose.setOnClickListener(v -> {

            if (downCartActivity != null) {
                downCartActivity.finish();
            }
            this.dismiss();
        });


    }

    /*** 下单成功 开始支付**/
    private void startPay() {
        JSONObject para = new JSONObject();
        /*** 购物车**/
        if (CONST.ORDER_TYPE_CART.equals(type)) {
            para.put("oCode", object.getMainCode());//主订单号（提交订单返回的mainCode）
            para.put("isQr", 0);//扫码支付（1） 非扫码支付（0）
        }
        /*** 立即购买**/
        if (CONST.ORDER_TYPE_NOW_BY.equals(type)) {
            para.put("oCode", object.getMainCode());//主订单号（提交订单返回的mainCode）
            para.put("orderId", object.getOrderIds());//订单id
            para.put("isQr", 0);//扫码支付（1） 非扫码支付（0）
        }
        new OkHttpKit().send(
                response200 -> {
                    JSONObject jsonObject = JSONObject.parseObject(response200);
                    if (jsonObject.getBoolean("success")) {
                        ToolsKit.payByNow(jsonObject, downCartActivity);
                    } else {
                        ToastUtils.showShort("支付失败");
                        payErro();
                    }
                },
                success401 -> {
                    ToastUtils.showShort("支付失败");
                    payErro();
                },
                error -> {
                    ToastUtils.showShort("支付失败");
                    payErro();
                }
        ).post(CONST.PAY_WEI_XIN + UserKit.getAccessToken(), para, getActivity());
    }

    /*** 支付失败回调**/
    public void payErro() {
        PayErroeActivity.setPara(object, type, downCartActivity, "", this);

        if (downCartActivity != null) {
            LogKit.E("payErro downCartActivity", "downCartActivity ");
            ToolsKit.go(PayErroeActivity.class);
        }
    }

    /*** 支付成功回调**/
    public void paySuccess() {
        PaySuccessActivity.setPara(object, type, downCartActivity, "", this);

        if (downCartActivity != null) {
            LogKit.E("paySuccess  downCartActivity  ", " downCartActivity ");
            ToolsKit.go(PaySuccessActivity.class);
        }

    }

    @Override
    public void onDestroy() {
        ToolsKit.deleteOKHttp(getActivity());
        super.onDestroy();
    }
}
